package com.runa.rccprotocol.dto.guangda;

import com.runa.protocol.dto.DtuReportAck;
import com.runa.protocol.util.ByteUtil;

/**
 * 2024/7/3 11:24
 * zhangYB
 */
public class GdFmuDataAck extends DtuReportAck {


    private byte startSymbol = 0x68;
    private byte endSymbol = 0x16;

    private String netEquNo;

    private byte[] clientId = new byte[5];

    private GdFmuControl control;

    public GdFmuDataAck(String netEquNo, GdFmuControl control) {
        this.netEquNo = netEquNo;
        this.control = control;

        byte[] frame = new byte[100];

        int index = 0;
        frame[index++] = startSymbol;

        byte[] bytes = ByteUtil.hexStringToByteFlip(netEquNo);
        for (byte b : bytes) {
            frame[index++] = b;
        }

        for (byte b : clientId) {
            frame[index++] = b;
        }

        frame[index++] = startSymbol;

        frame[index++] = control.getValue();

        byte[] dataLenBytes = ByteUtil.shortToByte(control.getDataLen());
        for (byte b : dataLenBytes) {
            frame[index++] = b;
        }

        byte[] frameData = control.getData();
        for (byte b : frameData) {
            frame[index++] = (byte)((b + 0x33) & 0xff);
        }

        int checksum = 0;
        for (int i = 0; i < index; i++) {
            checksum = (checksum + frame[i]) & 0xff;
        }
        frame[index++] = (byte)checksum;

        frame[index++] = endSymbol;

        this.data = ByteUtil.slice(frame, 0, index);
    }


}
